package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.MallOnlinePayWechatCallbackBo;
import com.rla.domain.vo.MallOnlinePayWechatCallbackVo;
import com.rla.domain.MallOnlinePayWechatCallback;
import com.rla.mapper.MallOnlinePayWechatCallbackMapper;
import com.rla.service.IMallOnlinePayWechatCallbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信支付结果Service业务层处理
 *
 * @author rla
 * @date 2025-08-12
 */
@RequiredArgsConstructor
@Service
public class MallOnlinePayWechatCallbackServiceImpl implements IMallOnlinePayWechatCallbackService {

    private final MallOnlinePayWechatCallbackMapper baseMapper;

    /**
     * 查询微信支付结果
     */
    @Override
    public MallOnlinePayWechatCallbackVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询微信支付结果列表
     */
    @Override
    public TableDataInfo<MallOnlinePayWechatCallbackVo> queryPageList(MallOnlinePayWechatCallbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOnlinePayWechatCallback> lqw = buildQueryWrapper(bo);
        Page<MallOnlinePayWechatCallbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询微信支付结果列表
     */
    @Override
    public List<MallOnlinePayWechatCallbackVo> queryList(MallOnlinePayWechatCallbackBo bo) {
        LambdaQueryWrapper<MallOnlinePayWechatCallback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallOnlinePayWechatCallback> buildQueryWrapper(MallOnlinePayWechatCallbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOnlinePayWechatCallback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), MallOnlinePayWechatCallback::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getTransactionId()), MallOnlinePayWechatCallback::getTransactionId, bo.getTransactionId());
        lqw.eq(bo.getTotal() != null, MallOnlinePayWechatCallback::getTotal, bo.getTotal());
        lqw.eq(bo.getPayerTotal() != null, MallOnlinePayWechatCallback::getPayerTotal, bo.getPayerTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrency()), MallOnlinePayWechatCallback::getCurrency, bo.getCurrency());
        lqw.eq(StringUtils.isNotBlank(bo.getPayerCurrency()), MallOnlinePayWechatCallback::getPayerCurrency, bo.getPayerCurrency());
        lqw.eq(StringUtils.isNotBlank(bo.getMchid()), MallOnlinePayWechatCallback::getMchid, bo.getMchid());
        lqw.eq(StringUtils.isNotBlank(bo.getOutTradeNo()), MallOnlinePayWechatCallback::getOutTradeNo, bo.getOutTradeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getTradeState()), MallOnlinePayWechatCallback::getTradeState, bo.getTradeState());
        lqw.eq(StringUtils.isNotBlank(bo.getTradeStateDesc()), MallOnlinePayWechatCallback::getTradeStateDesc, bo.getTradeStateDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), MallOnlinePayWechatCallback::getBankType, bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), MallOnlinePayWechatCallback::getAppid, bo.getAppid());
        lqw.eq(StringUtils.isNotBlank(bo.getTradeType()), MallOnlinePayWechatCallback::getTradeType, bo.getTradeType());
        lqw.eq(StringUtils.isNotBlank(bo.getAttach()), MallOnlinePayWechatCallback::getAttach, bo.getAttach());
        lqw.eq(StringUtils.isNotBlank(bo.getSuccessTime()), MallOnlinePayWechatCallback::getSuccessTime, bo.getSuccessTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), MallOnlinePayWechatCallback::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getPayReconciliationStatus()), MallOnlinePayWechatCallback::getPayReconciliationStatus, bo.getPayReconciliationStatus());
        return lqw;
    }

    /**
     * 新增微信支付结果
     */
    @Override
    public Boolean insertByBo(MallOnlinePayWechatCallbackBo bo) {
        MallOnlinePayWechatCallback add = BeanUtil.toBean(bo, MallOnlinePayWechatCallback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改微信支付结果
     */
    @Override
    public Boolean updateByBo(MallOnlinePayWechatCallbackBo bo) {
        MallOnlinePayWechatCallback update = BeanUtil.toBean(bo, MallOnlinePayWechatCallback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOnlinePayWechatCallback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除微信支付结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
